package com.jvup.common.support.base;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;

import com.jvup.common.support.DialogManage;

/**
 * 为了避免Splash广告不在对话框下面显示，把显示的对话框保存起来
 */
public class BaseDialog extends Dialog {

    private BaseActivity ownerActivity;

    public BaseDialog(@NonNull Context context) {
        this(context, 0);
    }

    public BaseDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);

        if(context instanceof BaseActivity) {
            ownerActivity = (BaseActivity) context;

            LifecycleObserverWrapper observerWrapper = new LifecycleObserverWrapper(this.ownerActivity);
            if(Lifecycle.State.CREATED.isAtLeast(this.ownerActivity.getLifecycle().getCurrentState())) {
                observerWrapper.activeStateChanged(true);
            }
            this.ownerActivity.getLifecycle().addObserver(observerWrapper);
        }
    }

    protected BaseDialog(@NonNull Context context, boolean cancelable, @Nullable DialogInterface.OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    public void show() {
        if(ownerActivity == null || ownerActivity.isDestroyed()){
            return;
        }
        super.show();
        DialogManage.add(this);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        DialogManage.remove(this);
    }

    public void closeDialogOnClick(View view) {
        dismiss();
    }

    class LifecycleObserverWrapper implements LifecycleEventObserver {

        @NonNull
        final LifecycleOwner mOwner;

        boolean mActive;

        boolean oldShow = false;

        LifecycleObserverWrapper(@NonNull LifecycleOwner owner) {
            mOwner = owner;
        }

        boolean isAttachedTo(LifecycleOwner owner) {
            return mOwner == owner;
        }

        boolean shouldBeActive() {
            return mOwner.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED);
        }

        @Override
        public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
            if (mOwner.getLifecycle().getCurrentState() == Lifecycle.State.DESTROYED) {
                detachObserver();
                activeStateChanged(false);
                dismiss();
                return;
            }
            activeStateChanged(shouldBeActive());
        }

        void activeStateChanged(boolean newActive) {
            if (newActive == mActive) {
                return;
            }
            // 设置活动状态
            mActive = newActive;

            if (mActive) {
                if(oldShow) {
                    show();
                }
            } else {
                // 获取是否显示状态
                oldShow = isShowing();
                dismiss();
            }
        }

        void detachObserver() {
            mOwner.getLifecycle().removeObserver(this);
        }
    }

}
